from selenium.webdriver.common.by import By

from common.basePage import Base


class ServiceRule(Base):
    X_add = "//span[@class='btn' and text()='添加']"
    X_rule_name = "//span[@class='field-name' and text()='规则名称']/following-sibling::input"
    X_min = "//span[@class='field-name' and text()='最小消费']/following-sibling::input"
    X_expect = "//span[@class='field-name' and text()='预估消费']/following-sibling::input"
    X_desc = "//span[@class='field-name' and text()='规则描述']/following-sibling::input"
    X_billing_type1 = "(//span[text()='计费单位']/following-sibling::input)[1]"
    X_price_type1 = "(//span[text()='单位价格']/following-sibling::input)[1]"
    X_service_num_type3 = "((//div[@class='fee-rate'])[last()]/input)[1]"
    X_billing_type3 = "((//div[@class='fee-rate'])[last()]/input)[2]"
    X_price_type3 = "((//div[@class='fee-rate'])[last()]/input)[3]"
    X_submit = "//span[text()='确定' and @class='btn']"
    X_select = "//select[@id='rule_type_id']"
    X_assert_text = "(//span[text()='规则名称'])[2]/following-sibling::span"
    X_add_fee_rate = "//button[text()='添加费率']"
    X_del_fee_rate = "((//div[@class='fee-rate'])[last()])/span[text()='X']"

    def goto_service_rule_page(self):
        self.base_click(by=By.LINK_TEXT, locate="业务规则")

    def rule_del_frist_item(self):
        self.base_del_first_item(by=By.CSS_SELECTOR,
                                 locate=".result-list-item:first-child .result-list-item-btn-bar span:first-child")

    def input_rule_data(self, loc, val):
        self.base_send(locate=loc, value=val)

    def get_text(self):
        return self.base_get_text(By.XPATH, self.X_assert_text)

    # def add_service_rule(self, name, ruleType, minFee, exceptFee, desc, bill, price, serNum, serNumList):
    def add_service_rule(self, name, ruleType, minFee, exceptFee, desc, feeRate):
        '''
        添加业务规则
        :param name: 规则名称
        :param ruleType: 规则类型
        :param minFee: 最小费用
        :param exceptFee: 预计费用
        :param desc: 描述
        :param bill: 费率，如果ruleType为
            预付费-下发业务量：不用填写
            预付费-下发费用：格式为['千瓦时','1']，元素分别是 单位、单价
            后付费-上报业务量：格式为[
                ['10L','小时','1'],
                ['20L','小时','2']]，每个元素分别是：业务码、单位、单价
        :param price: 价格单位
        :param serNum: 业务码
        :return:
        '''

        # 点击添加按钮
        if self.base_find(locate=self.X_add).text == "添加":
            self.base_click(locate=self.X_add)

        # 根据类型进行不同的操作
        self.input_rule_data(loc=self.X_rule_name, val=name)
        self.input_rule_data(loc=self.X_desc, val=desc)
        self.base_find_select(locate=self.X_select, value=ruleType)
        if ruleType == "预付费-下发业务量":
            self.input_rule_data(loc=self.X_min, val=minFee)
            self.input_rule_data(loc=self.X_expect, val=exceptFee)
            self.input_rule_data(loc=self.X_billing_type1, val=feeRate[0])
            self.input_rule_data(loc=self.X_price_type1, val=feeRate[1])
        elif ruleType == "预付费-下发费用":
            self.input_rule_data(loc=self.X_min, val=minFee)
            self.input_rule_data(loc=self.X_expect, val=exceptFee)
        elif ruleType == "后付费-上报业务量":
            for one in feeRate: # feeRate数据为：[[v1,v2,v3],[v1,v2,v3]] 形式
                self.input_rule_data(loc=self.X_service_num_type3, val=one[0])
                self.input_rule_data(loc=self.X_billing_type3, val=one[1])
                self.input_rule_data(loc=self.X_price_type3, val=one[2])
                self.base_click(locate=self.X_add_fee_rate)
            # 上面循环结束会多出一行费率数据，需要删除最后一条
            self.base_click(locate=self.X_del_fee_rate)

        # 点击确定按钮
        self.base_click(locate=self.X_submit)
        # 添加完成后，刷新页面
        self.driver.refresh()
        return self
